package leetcode;

import java.util.HashMap;

public class LC10 {
    public static void main(String[] args) {
        LC10 lc10 = new LC10();
        System.out.println(lc10.subarraySum(new int[]{1,-1,0},0));
        System.out.println(lc10.subarraySum(new int[]{28,54,7,-70,22,65,-6},100));
    }
    public int subarraySum1(int[] nums, int k) {
        int count = 0, pre = 0;
        HashMap < Integer, Integer > mp = new HashMap< >();
        mp.put(0, 1);
        for (int num : nums) {
            pre += num;
            if (mp.containsKey(pre - k)) {
                count += mp.get(pre - k);
            }
            mp.put(pre, mp.getOrDefault(pre, 0) + 1);
        }
        return count;
    }
    public int subarraySum(int[] nums, int k) {
        int ans = 0;
        for(int i = 0; i < nums.length; i++){
            int sum = nums[i];
//            if(sum > k) continue;
            if(sum == k) {ans++; continue;}
            for(int j = i+1; j < nums.length ;j++){
                sum+=nums[j];
                if(sum == k) {ans++; break;}
            }
        }
        return ans;
    }
}
